﻿using System.Diagnostics;
using System.IO;
using System.Security.Policy;
using System.Text.Json;
using YamlDotNet.Serialization;
using static Config;

public class Config
{
    // 静态配置属性（只读）
    public static string MongoUrl { get; private set; }
    public static string DataBase { get; private set; }
    public static string BookCollection { get; private set; }
    public static string BucketName { get; private set; }

    public static List<string> Dbs { get; set; }




    internal struct ConfigData
    {
        public string url { get; set; }
        public string dataBase { get; set; }
        public string bookCollection { get; set; }
        public string bucketName { get; set; }


    }

    // 静态加载方法（确保只调用一次）
    public static void Load(string filePath)
    {

        try
        {
            var config = File.ReadAllText(filePath);

            
            if (Path.GetExtension(filePath).ToLower() == ".yaml")
            {
                // 2. 创建 Deserializer 对象
                var deserializer = new DeserializerBuilder().Build();
                // 3. 反序列化 YAML 内容到 Config 对象
                ConfigData configData = deserializer.Deserialize<ConfigData>(config);
                //Dbs = configData.Dbs;
                MongoUrl = configData.url;
                DataBase = configData.dataBase;
                BookCollection = configData.bookCollection;
                BucketName = configData.bucketName??configData.bookCollection;
                Debug.WriteLine("加载完毕");
            }
        }
        catch (JsonException ex)
        {
            throw new Exception($"配置文件 {filePath} 解析失败: {ex.Message}", ex);
        }
    }

    // 禁止实例化
    private Config() { }
}
